package cacco.jp.mld;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cacco.jp.mld.panel.entity.UserEntity;
import cacco.jp.mld.panel.service.UserService;
@Controller
public class LoginController {

	
	@Autowired
    private UserService userService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String listEmployees(ModelMap map)
    {
		map.addAttribute("user",new UserEntity());
        return "login";
    }
	 @RequestMapping(value = "/login", method = RequestMethod.POST)
	public String addEmployee(@ModelAttribute(value="user") UserEntity user, BindingResult result,HttpServletRequest request)
	{
		  UserEntity entity  = userService.getUserByName(user);
		  request.getSession().setAttribute("aa", entity);
		  if(entity != null ){
			  return "redirect:/listUser";
		  }
		  return "login";
	       
	}
	@RequestMapping(value = "/denided", method = RequestMethod.GET)
	    public String loginerror(ModelMap model) {
	        model.addAttribute("error", "true");
	        return "denided";
	    }
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return "login";
    }
	@RequestMapping(value = "/error", method = RequestMethod.GET)
    public String error(ModelMap model) {
        return "error";
    }
	
}
